{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5.2\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "from paddle.nn import Linear\n",
    "import numpy as np\n",
    "import os\n",
    "import json\n",
    "import random\n",
    "print(paddle.__version__)\n",
    "\n",
    "from paddle.nn import Conv2D,MaxPool2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('data/mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "    train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labe=train_set[0].train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,label=val_sst[0],val_set[1]\n",
    "    elif model=='eval':\n",
    "        imgs,label=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100\n",
    "    def data_generator():\n",
    "        if mode=='train':\n",
    "            random.shuffle(index_list)\n",
    "            imgs_list=[]\n",
    "            labels_list=[]\n",
    "            for i in index_list:\n",
    "                img=np.array(imgs[i]).astype('float32')\n",
    "                label=np.reshape(labels[i],[1]).astype('int64')\n",
    "                imgs_list.append(img)\n",
    "                labels_list.append(label)\n",
    "                if len(imgs_list)==BATCHSIZE:\n",
    "                    yield np.array(imgs_list),np.array(labels_list)\n",
    "                    imgs_list=[]\n",
    "                    labels_list=[]\n",
    "            if len(imgs_list)>0:\n",
    "                yield np.array(imgs_list),np.array(labels_list)\n",
    "    return data_genertor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('data/mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "     train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labe=train_set[0].train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,label=val_sst[0],val_set[1]\n",
    "    elif model=='eval':\n",
    "        imgs,label=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
